﻿define(['app'], function (app) {
	'use strict';

	var controller = function controller($scope, $window, Sxqd, $ionicSlideBoxDelegate, GoPage, $ionicPopup, GoDialog, GoPicture, GoAddress, $timeout) {
		var store = new Array();
		$scope.input = new Object();
		$scope.rule = 1;
		$scope.input.count = 1;
		$scope.input.money = 0;
		$scope.input.memo = null;
		$scope.credits = new Object();
		$scope.input.store_id = null;
		$scope.input.store_select = { id: 0, name: "请选择店铺" };
		$scope.input.image = new Array();
		$scope.credits.credit_line = 0;
		$scope.credits.debt_total = 0;
		$scope.type1 = true;
		$scope.type2 = false;
		$scope.input.key = null;
		$scope.input.agent_nPage = 1;
		$scope.input.auditing_nPage = 1;
		$scope.input.passed_nPage = 1;
		$scope.input.unpass_nPage = 1;
		/***省市区三级联动***/
		$scope.input.province = { id: 0, name: "请选择省份" }; //省市区三级联动
		$scope.input.city = { id: 0, name: "请选择城市" }; //省市区三级联动
		$scope.input.district = { id: 0, name: "请选择区/县" }; //省市区三级联动
		//下拉侦听，目前只能省市区三级联动
		$scope.listen_province = function (data, obj) {
			$scope.input.city = { id: 0, name: "请选择城市" }; //省市区三级联动
			$scope.input.district = { id: 0, name: "请选择区/县" }; //省市区三级联动
		};
		$scope.listen_city = function (data, obj) {
			$scope.input.district = { id: 0, name: "请选择区/县" }; //省市区三级联动
		};
		//定义绑定
		var items = new Array();
		var obj1 = { name: 'input.province' };
		var obj2 = { name: 'input.city' };
		var obj3 = { name: 'input.district' };
		items.push(obj1);
		items.push(obj2);
		items.push(obj3);
		GoAddress.init_load($scope, items);
		$scope.doSelectProvince = function () {
			//id是值 name是显示
			GoDialog.showSelect("请选择省份", $scope.province_items, $scope.input.province, function (data) {
				$scope.input.province = data;
			});
		}
		$scope.doSelectCity = function () {
			//id是值 name是显示
			GoDialog.showSelect("请选择城市", $scope.city_items, $scope.input.city, function (data) {
				$scope.input.city = data;
			});
		}
		$scope.doSelectDistrict = function () {
			//id是值 name是显示
			GoDialog.showSelect("请选择区/县", $scope.district_items, $scope.input.district, function (data) {
				$scope.input.district = data;
			});
		}
		$scope.doSelectStore = function () {
			//id是值 name是显示
			//id是值 name是显示
			var list = new Array();
			$scope.datas.data.forEach(obj => {
				if (obj.id == undefined)
					obj.id = obj.store_id;
				if (obj.name == undefined)
					obj.name = obj.store_name;
				list.push(obj);
			});

			GoDialog.showSelect("请选择请选择店铺", list, $scope.input.store_select, function (data) {
				$scope.input.store_id = data.store_id;
				$scope.input.store_select = data;
			});
		}
		$scope.$on('$ionicView.afterEnter', function () {
			//动态设置高度
			var all = $window.innerHeight;
			//计算滚动条
			$scope.myObj = {
				"height": all - 44 + "px"
			};
			$scope.init_data();
		}, false);
		$scope.ondelete = function (index) {
			$scope.input.image.splice(index, 1);
		};
		//动态响应
		$scope.doShowFile = function () {
			document.getElementById("upsxqd_debt").click();
		}
		//图片上传
		$scope.onupload = function (val) {
			var fileList = val.files;
			for (var i = 0; i < val.files.length; i++) {
				var reader = new FileReader();
				reader.readAsDataURL(fileList[i]);
				var file = fileList[i];
				reader.onload = function (e) {
					//图片进行压缩处理
					GoPicture.compress(file, e.target.result, 50, function (base64) {
						$scope.$apply(function () {
							var i = $scope.input.image.length;
							$scope.input.image[i] = {
								"image": base64
							}
							val.value = "";
						});
					});
				}
			}
		};
		$scope.init_data = function () {
			Sxqd.supplier_search(null, null, 1, function (data) {
				if (data != null)
					$scope.datas = data;
				else
					$scope.datas = new Object();
				$scope.input.agent_nPage = 1;
				console.log(data);
				$scope.$watch('input.store_id', function (newValue, oldValue) {
					if (newValue != null && newValue != undefined)
						Sxqd.credit_list(newValue, 1, function (data) {
							if (data != null)
								$scope.credits = data;
							else
								$scope.credits = new Object();
							$scope.input.credits_nPage = 1;
							console.log(data);
							if (data != null)
								for (var i = 0; i < $scope.credits.credit.length; i++) {
									if ($scope.credits.credit[i].type == 1) {
										$scope.credits.credit[i].type_name = '线下支付';
									} else {
										$scope.credits.credit[i].type_name = '预存款';
									}
									if ($scope.credits.credit[i].status == 1) {
										$scope.credits.credit[i].status_name = '已确认';
									} else {
										$scope.credits.credit[i].status_name = '未确认';
									}
								}
							else {
								$scope.credits = new Object();
								$scope.credits.credit_line = 0;
								$scope.credits.debt_total = 0;
							}
						});
				})
			});
			Sxqd.agentapply_list(1, 1, function (data) {
				if (data != null)
					$scope.auditing = data;
				else
					$scope.auditing = new Object();
				$scope.input.auditing_nPage = 1;
				console.log(data);

			});
			Sxqd.agentapply_list(2, 1, function (data) {
				if (data != null)
					$scope.passed = data;
				else
					$scope.passed = new Object();
				$scope.input.passed_nPage = 1;
				console.log(data);

			});
			Sxqd.agentapply_list(3, 1, function (data) {
				if (data != null)
					$scope.unpass = data;
				else
					$scope.unpass = new Object();
				$scope.input.unpass_nPage = 1;
				console.log(data);

			});

		}
		//滑动时候触发
		$scope.selectIndex = 0;
		$scope.slideChanged = function (index) { //手指滑动
			$scope.selectIndex = index;
		};
		$scope.activeSlide = function (index) {
			$scope.selectIndex = index;
			$ionicSlideBoxDelegate.$getByHandle('main-handle').slide(index);
		};

		//还款记录、欠款记录
		$scope.isRepayment = true;
		$scope.isNotpay = false;
		$scope.change_pay = function () {
			$scope.isRepayment = true;
			$scope.isNotpay = false;
		}
		$scope.change_notpay = function () {
			$scope.isNotpay = true;
			$scope.isRepayment = false;
		}

		//审核中、通过、未通过
		$scope.looking = true;
		$scope.past = false;
		$scope.notpast = false;
		$scope.change_looking = function () {
			$scope.looking = true;
			$scope.past = false;
			$scope.notpast = false;
		}
		$scope.change_past = function () {
			$scope.looking = false;
			$scope.past = true;
			$scope.notpast = false;
		}

		$scope.change_notpast = function () {
			$scope.looking = false;
			$scope.past = false;
			$scope.notpast = true;
		}

		$scope.doSearch = function () {
			$scope.get_region();
			console.log($scope.region);
			Sxqd.supplier_search($scope.input.key, $scope.region, function (data) {
				if (data != null)
					$scope.datas = data;
				else
					$scope.datas = new Object();
				$scope.input.agent_nPage = 1;

			});
		}
		$scope.change_type = function (index) {
			$scope.type = index;
		}
		$scope.get_region = function () {
			$scope.region = null;
			if ($scope.input.province != null) {
				$scope.region = $scope.input.province.id;
			}
			if ($scope.input.city != null) {
				$scope.region += ',' + $scope.input.city.id;
			}
			if ($scope.input.district != null) {
				$scope.region += ',' + $scope.input.district.id;
			}
		}
		//点击弹出详情
		$scope.notlist = ["AA", 'BB', 'CC'];
		$scope.scope = new Object();
		var myPopup;
		$scope.showPopup = function (store_id) { //绑定数据时要注意，如果是数组，则通过$index传入下标；如果是json，则传入key

			Sxqd.product_detail(store_id, function (data) {
				$scope.detail = data;
				console.log(data);
				myPopup = $ionicPopup.show({ //show()
					templateUrl: 'templates/modal/sxqd_detail.html',
					title: "制造商详情",
					scope: $scope, //绑定数据，必须写这行
					buttons: [{
						text: "确定",
						type: "button-assertive",
						onTap: function () { // 当点击保存按钮时，执行该函数
							//						console.log($scope.scope.reason);
							//return $scope.scope.reason;//
						}
					}]
				});
			});

		}
		var myPopup_debt;
		$scope.showPopup_debt = function (store_id) { //绑定数据时要注意，如果是数组，则通过$index传入下标；如果是json，则传入key
			$scope.type = '';
			$scope.input.money = 0;
			Sxqd.agent_credit(store_id, function (data) {
				$scope.credit = data;
				$scope.$watch('input.money', function (newValue, oldValue) {
					console.log(newValue);
					if (newValue > $scope.credit.debt_total)
						$scope.input.money = parseFloat($scope.credit.debt_total);
				});
				myPopup_debt = $ionicPopup.show({
					templateUrl: 'templates/modal/sxqd_debt.html',
					title: "还款",
					scope: $scope, //绑定数据，必须写这行
					buttons: [{
						text: "取消",
						type: "button-assertive",
						onTap: function () { // 点击取消按钮时，执行该函数
							$scope.input.image = new Array();
						}
					}, {
						text: "确定",
						type: "button-positive",
						onTap: function (e) { // 当点击保存按钮时，执行该函数

							if ($scope.input.money == 0 || $scope.input.money == null) {
								GoDialog.toast("请输入还款金额");
								e.preventDefault();
								return
							}
							if ($scope.type == undefined) {
								GoDialog.toast("请选择还款方式");
								e.preventDefault();
								return;
							}
							if ($scope.type == 1) {
								if ($scope.input.image.length < 1) {
									GoDialog.toast("请上传还款凭证");
									e.preventDefault();
									return;
								}
							}
							if ($scope.type == 2)
								if ($scope.input.money > $scope.credit.advance) {
									GoDialog.toast("余额不足");
									e.preventDefault();
									return;
								}
							Sxqd.credit_repay(store_id, $scope.input.money, $scope.type, $scope.input.image, function (data) {
								if (data.success == true) {
									GoDialog.toast("还款成功");
									$scope.input.image = new Array();

								} else
									GoDialog.toast(data.msg);
							})
						}
					}]
				});
			})

		}
		$scope.rule_choose = function (index) {
			$scope.rule = index;
		}
		var myPopup_red;
		$scope.showPopup_red = function (val) { //绑定数据时要注意，如果是数组，则通过$index传入下标；如果是json，则传入key
			$scope.input.money = 1;
			$scope.input.count = 1
			myPopup_red = $ionicPopup.show({ //show()
				templateUrl: 'templates/modal/give_redbag.html',
				title: "发红包",
				scope: $scope, //绑定数据，必须写这行
				buttons: [{
					text: "取消",
					type: "button-balanced",
					onTap: function () { // 当点击取消按钮时，执行该函数

					}
				}, {
					text: "确定",
					type: "button-assertive",
					onTap: function (e) { // 当点击保存按钮时，执行该函数
						//console.log($scope.scope.reason);
						//return $scope.scope.reason;//
						if ($scope.input.money == 0) {
							GoDialog.toast("请输入红包金额");
							e.preventDefault();
							return;
						} else if ($scope.input.count < 1) {
							GoDialog.toast("请输入红包数量");
							e.preventDefault();
							return;

						} else {
							store = new Array;
							var n = store.length;
							store[n] = {
								"store_id": val
							};
							Sxqd.packets_grant($scope.input.count, $scope.input.money, $scope.input.memo, $scope.rule, store, function (data) {
								if (data.success == true) {
									GoDialog.toast("发送成功");
								} else {
									GoDialog.toast("发送失败");
									e.preventDefault();
								}

							})
						}
					}
				}]
			});
		};

		//点击其他区域关闭弹出窗       
		var htmlEl = angular.element(document.querySelector('html'));
		htmlEl.on('click', function (event) {
			if (event.target.nodeName === 'HTML') {
				if (myPopup) {
					myPopup.close();
				}
				if (myPopup_red) {
					myPopup_red.close();
				}
				if (myPopup_debt) {
					myPopup_debt.close();
				}
			}
		});
		$scope.isLoad = false;
		$scope.doRefresh = function () {
			$scope.isLoad = true;
			if ($scope.selectIndex == 0) {
				Sxqd.supplier_search($scope.input.key, $scope.region, 1, function (data) {
					if (data != null)
						$scope.datas = data;
					else
						$scope.datas = new Object();
					$scope.input.agent_nPage = 1;
					console.log(data);
				}, function () {
					$scope.$broadcast('scroll.refreshComplete');
					$timeout(function () {
						$scope.isLoad = false;
						console.log($scope.isLoad);
					}, 1500);
				});
			} else if ($scope.selectIndex == 1) {
				Sxqd.credit_list($scope.input.store_id, 1, function (data) {
					if (data != null) {
						$scope.credits = data;
						for (var i = 0; i < $scope.credits.credit.length; i++) {
							if ($scope.credits.credit[i].type == 1) {
								$scope.credits.credit[i].type_name = '线下支付';
							} else {
								$scope.credits.credit[i].type_name = '预存款';
							}
							if ($scope.credits.credit[i].status == 1) {
								$scope.credits.credit[i].status_name = '已确认';
							} else {
								$scope.credits.credit[i].status_name = '未确认';
							}
						}
					} else {

						$scope.credits = new Object();
						$scope.credits.credit_line = 0;
						$scope.credits.debt_total = 0;
					}
					$scope.input.credits_nPage = 1
				}, function () {
					$scope.$broadcast('scroll.refreshComplete');
					$timeout(function () {
						$scope.isLoad = false;
						console.log($scope.isLoad);
					}, 1500);
				});
			} else {
				if ($scope.looking == true) {
					Sxqd.agentapply_list(1, 1, function (data) {
						if (data != null)
							$scope.auditing = data;
						else
							$scope.auditing = new Object();
						$scope.input.auditing_nPage = 1;
						console.log(data);

					}, function () {
						$scope.$broadcast('scroll.refreshComplete');
						$timeout(function () {
							$scope.isLoad = false;
							console.log($scope.isLoad);
						}, 1500);
					});

				} else if ($scope.past == true) {
					Sxqd.agentapply_list(2, 1, function (data) {
						if (data != null)
							$scope.passed = data;
						else
							$scope.passed = new Object();
						$scope.input.passed_nPage = 1;
						console.log(data);

					}, function () {
						$scope.$broadcast('scroll.refreshComplete');
						$timeout(function () {
							$scope.isLoad = false;
						}, 1500);
					});
				} else {
					Sxqd.agentapply_list(3, 1, function (data) {
						if (data != null)
							$scope.unpass = data;
						else
							$scope.unpass = new Object();
						$scope.input.unpass_nPage = 1;
						console.log(data);

					}, function () {
						$scope.$broadcast('scroll.refreshComplete');
						$timeout(function () {
							$scope.isLoad = false;
						}, 1500);
					});
				}
			}

		}
		$scope.doRefreshMore = function () {
			$scope.isLoad = true;
			if ($scope.selectIndex == 0) {
				if ($scope.datas.data == undefined)
					$scope.inut.agent_nPage = 0;
				Sxqd.supplier_search($scope.input.key, $scope.region, $scope.input.agent_nPage + 1, function (data) {
					if (data != null) {
						if (data.data.length == 0)
							return;
						if ($scope.datas.data != undefined) {
							$scope.datas.data = $scope.datas.data.concat(data.data);
							$scope.input.agent_nPage += 1;
						} else {
							$scope.datas = data;
							$scope.input.agent_nPage = 1;
						}
					}
				}, function () {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function () {
						$scope.isLoad = false;
						console.log($scope.isLoad);
					}, 1500);
				});
			} else if ($scope.selectIndex == 1) {
				if ($scope.credits.credit == undefined)
					$scope.input.credits_nPage = 0;
				Sxqd.credit_list($scope.input.store_id, $scope.input.credits_nPage + 1, function (data) {
					console.log(data);
					if (data != null) {
						if (data.credit.length == 0)
							return;
						if ($scope.credits.credit != undefined) {
							$scope.credits.credit = $scope.credits.credit.concat(data.credit);
							$scope.input.credits_nPage += 1;
							for (var i = 0; i < $scope.credits.credit.length; i++) {
								if ($scope.credits.credit[i].type == 1) {
									$scope.credits.credit[i].type_name = '线下支付';
								} else {
									$scope.credits.credit[i].type_name = '预存款';
								}
								if ($scope.credits.credit[i].status == 1) {
									$scope.credits.credit[i].status_name = '已确认';
								} else {
									$scope.credits.credit[i].status_name = '未确认';
								}
							}
						} else {
							$scope.input.credits_nPage = 1
							$scope.credits = data;
							$scope.credits.credit_line = 0;
							$scope.credits.debt_total = 0;
						}

					}

				}, function () {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function () {
						$scope.isLoad = false;
					}, 1500);
				});
			} else {
				if ($scope.looking == true) {
					if ($scope.auditing.apply == undefined)
						$scope.input.auditing_nPage = 0;
					Sxqd.agentapply_list(1, $scope.input.auditing_nPage + 1, function (data) {
						if (data != null) {
							if (data.apply.length == 0)
								return;
							if ($scope.auditing.apply != undefined) {
								$scope.auditing.apply = $scope.auditing.apply.concat(data.apply);
								$scope.input.auditing_nPage += 1;
							} else {
								$scope.auditing = data;
								$scope.input.auditing_nPage = 1;
							}
						}

					}, function () {
						$scope.$broadcast('srcoll.infiniteScrollComplete');
						$timeout(function () {
							$scope.isLoad = false;
						}, 1500);
					});

				} else if ($scope.past == true) {
					if ($scope.passed.apply == undefined)
						$scope.input.passed_nPage = 0;
					Sxqd.agentapply_list(2, $scope.input.passed_nPage + 1, function (data) {
						if (data != null) {
							if (data.apply.length == 0)
								return;
							if ($scope.passed.apply != undefined) {
								$scope.passed.apply = $scope.passed.apply.concat(data.apply);
								$scope.input.passed_nPage += 1;
							} else {
								$scope.passed = data;
								$scope.input.passed_nPage = 1;
							}
						}

					}, function () {
						$scope.$broadcast('srcoll.infiniteScrollComplete');
						$timeout(function () {
							$scope.isLoad = false;
						}, 1500);
					});
				} else {
					if ($scope.unpass.apply == undefined)
						$scope.input.unpass_nPage = 0;
					Sxqd.agentapply_list(3, $scope.input.unpass_nPage + 1, function (data) {
						if (data != null) {
							if (data.apply.length == 0)
								return;
							if ($scope.unpass.apply != undefined) {
								$scope.unpass.apply = $scope.unpass.apply.concat(data.apply);
								$scope.input.unpass_nPage += 1;
							} else {
								$scope.unpass = data;
								$scope.input.unpass_nPage = 1;
							}
						}

					}, function () {
						$scope.$broadcast('srcoll.infiniteScrollComplete');
						$timeout(function () {
							$scope.isLoad = false;
						}, 1500);
					});
				}

			}
		}

	}

	controller.$inject = ['$scope', '$window', 'Sxqd', '$ionicSlideBoxDelegate', 'GoPage', '$ionicPopup', 'GoDialog', 'GoPicture', 'GoAddress', '$timeout'];

	return app.registerController("sxqdctrl", controller);
});